#include "GribDataReader.h"
#include "GribVariable.h"
#include "GribSyncFile.h"

namespace OCPP {
	GribDataReader::GribDataReader(GridShape origin, GridShape shape, GridVariablePtr variable)
		:GridDataReader()
	{
		_origin = origin;
		_shape = shape;
		_variable = variable;
	}

	GridDataValuePtr GribDataReader::read(GridData& gridData, const Volume::VolumeOptions& options)
	{
		GridDataValuePtr gridDataValue = std::make_shared<GridDataValue>();

		GribVariable* var
			= static_cast<GribVariable*>(_variable.get());
		
		auto sharedPtr = var->gridSyncFilePtr.lock();
		if (sharedPtr == nullptr) return nullptr;

		auto varData = sharedPtr->getData(var, _origin, _shape, options);
		gridDataValue->_vecDouble = std::move(varData.dv);

		gridData.extent.dimX = varData.Nx;
		gridData.extent.dimY = varData.Ny;
		gridData.extent.rectangle.west = Cesium::CesiumMath::toRadians(varData.lon1);
		gridData.extent.rectangle.east = Cesium::CesiumMath::toRadians(varData.lon2);
		gridData.extent.rectangle.south = Cesium::CesiumMath::toRadians(varData.lat2);
		gridData.extent.rectangle.north = Cesium::CesiumMath::toRadians(varData.lat1);

		return gridDataValue;
	}

}